<template>
	<view class="detail-container">
		<!-- 头部信息卡片 -->
		<view class="header-card">
			<view class="url-info">
				<text class="title">{{ urlInfo.urlDesc || '未命名链接' }}</text>
				<text class="url">{{ urlInfo.sourceUrl }}</text>
			</view>
			<view class="stats">
				<view class="stat-item">
					<u-icon name="eye" size="40" color="#2979ff"></u-icon>
					<text class="value">{{ urlInfo.accCount }}</text>
					<text class="label">总点击</text>
				</view>
				<view class="stat-item">
					<u-icon name="calendar" size="40" color="#2979ff"></u-icon>
					<text class="value">{{ formatDate(urlInfo.createTime) }}</text>
					<text class="label">创建日期</text>
				</view>
			</view>
		</view>

		<!-- IP分组列表 -->
		<view class="ip-section" v-if="false">
			<view class="section-header">
				<text class="section-title">访问记录</text>
				<text class="section-subtitle">按IP地址分组</text>
			</view>

			<u-collapse accordion>
				<u-collapse-item v-for="(group, index) in ipGroups" :key="index"
					:title="`${group.ip} (${group.count}次)`">
					<view class="access-list">
						<view v-for="(record, idx) in group.records" :key="idx" class="access-item">
							<view class="access-time">
								<u-icon name="clock" size="24" color="#909399"></u-icon>
								<text>{{ formatDateTime(record.time) }}</text>
							</view>
							<view class="access-device">
								<u-icon name="smartphone" size="24" color="#909399"></u-icon>
								<text>{{ record.device || '未知设备' }}</text>
							</view>
						</view>
					</view>
				</u-collapse-item>
			</u-collapse>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				urlInfo: {},
				ipGroups: []
			}
		},
		onLoad(options) {
			try {
				const urlInfo = uni.getStorageSync('URLDETAIL');
				if (urlInfo) {
					this.urlInfo = JSON.parse(urlInfo)
					this.loadUrlDetail()
				}
			} catch (e) {
				// error
			}
		},
		methods: {
			loadUrlDetail() {
				this.$api.user.queryAccLogBySurl({
					accurl: this.urlInfo.sourceUrl
				}).then((res) => {
					console.log(res);
					this.ipGroups = this.groupBy(res,'ip');
				})
				// 模拟IP分组数据
				// this.ipGroups = this.generateMockIpGroups()
			},
			// generateMockIpGroups() {
			// 	const groups = []
			// 	const ipCount = this.urlInfo.accCount;

			// 	for (let i = 0; i < ipCount; i++) {
			// 		const recordCount = 1 + Math.floor(Math.random() * 5)
			// 		const records = []

			// 		for (let j = 0; j < recordCount; j++) {
			// 			records.push({
			// 				time: Date.now() - Math.floor(Math.random() * 7 * 24 * 60 * 60 * 1000),
			// 				device: ['iPhone', 'Android', 'Windows', 'Mac'][Math.floor(Math.random() * 4)]
			// 			})
			// 		}

			// 		groups.push({
			// 			ip: `192.168.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}`,
			// 			count: recordCount,
			// 			records: records
			// 		})
			// 	}

			// 	return groups
			// },
			groupBy(arr, key) {
				return arr.reduce((acc, obj) => {
					const groupKey = obj[key];
					if (!acc[groupKey]) {
					  acc[groupKey] = [];
					}
					acc[groupKey].push(obj);
					return acc;
				}, {});
			},
			formatDate(timestamp) {
				const date = new Date(timestamp)
				return `${date.getFullYear()}-${(date.getMonth()+1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`
			},
			formatDateTime(timestamp) {
				const date = new Date(timestamp)
				return `${this.formatDate(timestamp)} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`
			}
		}
	}
</script>

<style lang="scss">
	.detail-container {
		padding: 20rpx;
		background-color: #f5f7fa;
		min-height: 100vh;

		.header-card {
			background-color: #ffffff;
			border-radius: 16rpx;
			padding: 30rpx;
			margin-bottom: 20rpx;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);

			.url-info {
				margin-bottom: 30rpx;

				.title {
					display: block;
					font-size: 36rpx;
					font-weight: bold;
					color: #303133;
					margin-bottom: 10rpx;
				}

				.url {
					display: block;
					font-size: 28rpx;
					color: #2979ff;
					word-break: break-all;
				}
			}

			.stats {
				display: flex;
				justify-content: space-around;
				border-top: 1rpx solid #f1f1f1;
				padding-top: 30rpx;

				.stat-item {
					display: flex;
					flex-direction: column;
					align-items: center;

					.value {
						font-size: 32rpx;
						font-weight: bold;
						color: #303133;
						margin: 10rpx 0;
					}

					.label {
						font-size: 24rpx;
						color: #909399;
					}
				}
			}
		}

		.ip-section {
			background-color: #ffffff;
			border-radius: 16rpx;
			padding: 30rpx;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);

			.section-header {
				margin-bottom: 20rpx;

				.section-title {
					display: block;
					font-size: 32rpx;
					font-weight: bold;
					color: #303133;
				}

				.section-subtitle {
					display: block;
					font-size: 26rpx;
					color: #909399;
					margin-top: 10rpx;
				}
			}

			.access-list {
				padding: 20rpx 0;

				.access-item {
					display: flex;
					justify-content: space-between;
					padding: 20rpx 0;
					border-bottom: 1rpx dashed #eee;

					&:last-child {
						border-bottom: none;
					}

					.access-time,
					.access-device {
						display: flex;
						align-items: center;
						font-size: 26rpx;
						color: #606266;

						.u-icon {
							margin-right: 10rpx;
						}
					}
				}
			}
		}
	}
</style>